package patrol.mobile.com.mobilepatrol.activity.rong;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.rong.callkit.RongCallKit;
import io.rong.imkit.RongIM;
import io.rong.imlib.model.Conversation;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.utils.Constants;
import patrol.mobile.com.mobilepatrol.utils.SPUtils;

public class SetTokenActivity extends BaseActivity {

    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_rong_set_token;
    }

    @Override
    public void setListener() {

    }

    @Override
    public void initTitle() {
        super.initTitle();
        tv_center_title.setVisibility(View.VISIBLE);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_center_title.setText("联系人列表");
        getUsers();
    }

    @Override
    public void initData() {
        super.initData();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    public void getUsers() {
        OkHttpUtils.get().url(SPUtils.get(Constants.current_ser, "") + Urls.getUsers).build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        try {
                            JSONObject object = new JSONObject(response);
                            if (object.getInt("state") == 200) {
                                mRecyclerView.setAdapter(new UserInfoAdapter(object.getJSONArray("yhList")));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });
    }

    public class UserInfoAdapter extends RecyclerView.Adapter<UserInfoAdapter.ViewHolder> {
        private JSONArray jsonArray;

        public UserInfoAdapter(JSONArray jsonArray) {
            this.jsonArray = jsonArray;
        }

        @Override
        public UserInfoAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_userinfo_laout, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(UserInfoAdapter.ViewHolder holder, int position) {
            try {
                JSONObject jsonObject = jsonArray.getJSONObject(position);
                final String userName = jsonObject.getString("ryname");
                final String ryid = jsonObject.getString("ryid");
                holder.userType.setText(jsonObject.getString("bmname"));
                holder.userName.setText(userName);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        startActivity(new Intent(v.getContext(), ConversationActivity.class)
//                                .putExtra("title", userName)
//                                .putExtra("targetId", ryid)
//                        );
                        RongIM.getInstance().startConversation(SetTokenActivity.this, Conversation.ConversationType.PRIVATE, ryid, userName);
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public int getItemCount() {
            return jsonArray.length();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.userName)
            TextView userName;
            @BindView(R.id.userType)
            TextView userType;

            ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }
        }
    }
}
